/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Form, Input, Button } from 'antd';
import type { ConnectState } from '@/models/connect';
import { connect } from 'umi';
import type { VerCodeSearchIndexType } from '../types/VerCodeSearch';
import CommonTable from './_common/CommonTable';
import { VerCodeSearchColumns } from './utils';
import CommonResetButton from './_common/CommonResetButton';
const FormItem = Form.Item;

interface FormType {
  send_number: string;
}

const VerCodeSearch = (props: VerCodeSearchIndexType) => {
  const { screenSize, dispatch, tableLoading, verCodeList, isEmpty } = props;
  const [form] = Form.useForm();
  const onFinish = (values: FormType) => {
    dispatch({
      type: 'customerSearch/getVerCodeList',
      payload: { ...values },
    });
  };

  const onReset = () => {
    form.resetFields();
    dispatch({
      type: 'customerSearch/save',
      payload: {
        verCodeList: [],
        isEmptyVerCodeList: false,
      },
    });
  };
  return (
    <>
      <Form
        form={form}
        onFinish={onFinish}
        layout={screenSize ? 'vertical' : 'inline'}
        style={{ marginBottom: 20 }}
      >
        <FormItem
          label={'手机号'}
          name='send_number'
          rules={[{ required: true, message: '请输入手机号' }]}
        >
          <Input allowClear style={{ width: 200 }} placeholder={'请输入手机号'} />
        </FormItem>
        <FormItem>
          <Button type='primary' htmlType='submit' loading={false}>
            查询
          </Button>
          <CommonResetButton onReset={onReset} />
        </FormItem>
      </Form>
      {!!verCodeList.length && (
        <>
          <CommonTable
            loading={tableLoading}
            datasource={verCodeList || []}
            columns={VerCodeSearchColumns.dx}
          />
          <div>*最多显示30前30条数据</div>
        </>
      )}
      {isEmpty && <div>无记录</div>}
    </>
  );
};

export default connect(({ customerSearch, loading }: ConnectState) => ({
  verCodeList: customerSearch.verCodeList || [],
  isEmpty: customerSearch.isEmptyVerCodeList || false,
  tableLoading: loading.effects['customerSearch/sms'] || false,
}))(React.memo(VerCodeSearch));
